// pages/device/list/index.js
var app = getApp(), fetchApi = require("../../../utils/httpPost.js")
import Toast from '../../../component/vant/toast/toast';
import Dialog from '../../../component/vant/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mersList:[],
    lng:0,
    lat:0,
    pageSize:10,
    pageIndex: 1,
    netWork:false,
    loading:false,
    hasData:false,
    isLoading:true,
    keyword:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.modalcnt()
  },
  modalcnt:function() {
    var that = this;
    //获取经纬度  
    wx.getLocation({
      type:  'gcj02',
      success:  function  (res)  {
        console.log('modalcnt', res)
        var latitude = res.latitude
        var longitude = res.longitude
        that.data.wd = latitude//纬度
        that.data.jd = longitude//经度
        that.setData({
          wd : latitude,
          jd : longitude
        })
        app.setStorageSync("lng",longitude);
        app.setStorageSync("lat",latitude);
        that.onHttpInit();
      },fail:function(res){
        that.data.wd = 0
        that.data.jd = 0
        that.onHttpInit();
      }
    })
  },
  onHttpInit: function () {
    var that = this;
    fetchApi.httpPost('/mer/getEgg/list', {
      keyword:that.data.keyword,
      longitude: that.data.jd,
      latitude: that.data.wd,
      pageIndex:that.data.pageIndex,
      pageSize:that.data.pageSize,
      shop_type:1,
    }, function (res) {
      console.log("获取附近的设备", res)
      if (res.data.code == 0) {
        if (res.data.totalcount > 0) {
          that.data.pageIndex = that.data.pageIndex + 1
          var mers = res.data.data.mers;
          if(mers.length > 0){
            console.log('devices',mers)
            for (var index in mers) {
              that.data.mersList.splice(that.data.mersList.length, 0, mers[index]);
            }
            that.setData({
              hasData: true,
              mersList: that.data.mersList
            })
          }
        }
      }else{
        Toast.fail(res.data.msg)
      }
      
      // complete
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
      that.setData({ netWork: true,loading: true,  isLoading: false, })
    }, function () {
      that.setData({ netWork: false, hasData: false,loading: true,  isLoading: false, })
    })
  },
  goLocation:function(e){
    var that = this;
    var isLogin = app.getStorageSync("isLogin");
    if (!isLogin) {
      wx.navigateTo({
        url: '/pages/login/index',
      })
    }else {
      console.log('goLocation',e)
      var index = e.currentTarget.dataset.index;
      console.log('device',this.data.mersList[index])
      var latitude = this.data.mersList[index].latitude!=undefined ? this.data.mersList[index].latitude : 0;
      var longitude = this.data.mersList[index].longitude!=undefined ? this.data.mersList[index].longitude : 0;
      if(latitude > 0 && longitude > 0){
        wx.openLocation({
          latitude,
          longitude,
          scale: 17
        })
      }else{
        Toast.fail("该设备未设置定位信息")
      }
    }
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.refreshdeviceList()
  },
  refreshdeviceList:function(){
    this.setData({
      loading: false,
      netWork: false,
      isLoading: true,
      hasData:false,
      mersList:[]
    })
    this.data.pageIndex = 1
    this.onHttpInit()
  },
  onChange(val){
    console.log('onSearch',val)
    this.data.keyword = val.detail;
    this.setData({
      keyword: val.detail
    })
  },
  onSearchClick(val){
    console.log('onSearchClick',val)
    this.refreshdeviceList()
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      isLoading: true
    })
    this.onHttpInit()
  },
})